package com.jumi.microservice.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jumi.microservice.entity.CpsUserWallet;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author scw
 * @date 2021/1/8
 */
@Mapper
public interface CpsUserWalletMapper extends BaseMapper<CpsUserWallet> {

    /**
     * 批量插入
     * @param list
     * @return
     */
    @Insert("<script>INSERT INTO cps_user_wallet "
            + "(amount,type,remark,user_id,order_id,order_no)"
            + "VALUES "
            + "<foreach collection='list' item='w' index='index' separator=',' >"
            + "(#{w.amount},#{w.type},#{w.remark},#{w.userId},#{w.orderId},#{w.orderNo})" +
            "</foreach></script>")
    int batchInsert(List<CpsUserWallet> list);

    /**
     * 获取该用户余额
     * @param userId
     * @return
     */
    @Select("select IFNULL(SUM(amount),0) from cps_user_wallet where user_id = #{userId}")
    BigDecimal getBalanceByUserId(@Param("userId") Long userId);

    /**
     * 可提现总额
     * @param userIdList
     * @return
     */
    @Select("<script>select IFNULL(sum(amount),0) from cps_user_wallet <if test='userIdList !=null and userIdList.trim()!=&quot;&quot;'>where user_id in ( ${userIdList} )</if></script>")
    BigDecimal totalWaitCash(@Param("userIdList") String userIdList);

    /**
     * 根据用户ID获取可提现金额
     * @param userId
     * @return
     */
    @Select("select ifnull(sum(amount),0) from cps_user_wallet where user_id = #{user_id}")
    BigDecimal WaitCashByUserId(@Param("user_id") Long userId);

    /**
     * 用户ID获取今天可提现金额
     * @param userId
     * @return
     */
    @Select("select ifnull(sum(amount),0) from cps_user_wallet where user_id = #{user_id} and TO_DAYS(create_time) = TO_DAYS(NOW())")
    BigDecimal WaitCashByUserIdToday(@Param("user_id") Long userId);


    /**
     * 根据用户ID获取已提现金额
     * @param userId
     * @return
     */
    @Select("select ifnull(sum(amount),0) from cps_user_wallet where type in(202,107)  and user_id = #{user_id}")
    BigDecimal AlreadyCashByUserId(@Param("user_id") Long userId);

    /**
     * 根据用户ID列表获取已提现金额
     * @param userIdList
     * @return
     */
    @Select("<script>" +
            "select " +
            "user_id as userId,ifnull(sum(amount),0) as amount " +
            "from cps_user_wallet " +
            "where type in(202,107)  " +
            "and user_id in  ( ${userIdList} ) " +
            "group by user_id"+
            "</script>"
    )
    List<Map<String,Object>> AlreadyCashByUserIdList(@Param("userIdList") String userIdList);

    /**
     * 根据用户ID列表获取可提现总额
     * @param userIdList
     * @return
     */
    @Select("<script>" +
            "select " +
            "user_id as userId,ifnull(sum(amount),0) as amount " +
            "from cps_user_wallet " +
            "where " +
            "user_id in  ( ${userIdList} ) " +
            "group by user_id"+
            "</script>"
    )
    List<Map<String,Object>> waitCashByUserIdList(@Param("userIdList") String userIdList);

    /**
     * 根据时间段统计用可用余额
     * @return
     */
    @Select("select user_id as userId,ifnull(sum(amount),0) as amount from cps_user_wallet where TO_DAYS(create_time) < TO_DAYS(NOW()) group by userId")
    List<Map<String,BigDecimal>> listDayWaitCashByTime();

    /**
     * 根据时间段统计用可用余额
     * @return
     */
    @Select("select ifnull(sum(amount),0) from cps_user_wallet where TO_DAYS(create_time) < TO_DAYS(NOW()) and user_id = #{uid}")
    BigDecimal listDayWaitCashByTimeByUid(@Param("uid") Long uid);
}
